package com.atguigu.teach.juc;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Ticket
{
    int number = 50;//模拟50张票

    /*public synchronized void sale()
    {
        if(number > 0)
        {
            System.out.println(Thread.currentThread().getName()+"\t"+"卖出第："+(number--)+"\t 还剩下： "+number);
        }
    }*/

    //==========================================

    //private final ReentrantLock lock = new ReentrantLock();  juc
    private Lock lock = new ReentrantLock();
    public void sale()
    {
        lock.lock();
        try
        {
            if(number > 0)
            {
                System.out.println(Thread.currentThread().getName()+"\t"+"卖出第："+(number--)+"\t 还剩下： "+number);
            }
        }finally {
            lock.unlock();
        }
    }

}

/**
 * @auther zzyy
 * @create 2020-03-25 12:41
 * 三个售票员    卖出      50张票
 * 如何编写企业级需要的工程化代码？
 *
 * 1 在高内聚，低耦合的前提下，线程        操作(卖票)      资源类。
 */
public class SaleTicketDemo
{
    public static void main(String[] args)//一切程序的入口
    {
        Ticket ticket = new Ticket();

        //官网文档，api手册
        // Thread(Runnable target, String name) Allocates a new Thread object.

        /*new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                for (int i = 0; i < 51; i++) {
                    ticket.sale();
                }
            }
        }, "a").start();

        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                for (int i = 0; i < 51; i++) {
                    ticket.sale();
                }
            }
        }, "b").start();

        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                for (int i = 0; i < 51; i++) {
                    ticket.sale();
                }
            }
        }, "c").start();*/


        new Thread(() -> {for (int i = 0; i < 51; i++) ticket.sale();}, "a").start();
        new Thread(() -> {for (int i = 0; i < 51; i++) ticket.sale();}, "b").start();
        new Thread(() -> {for (int i = 0; i < 51; i++) ticket.sale();}, "c").start();


        /*@FunctionalInterface
        public interface Runnable {

            public abstract void run();
        }*/
        // Thread(Runnable target, String name) Allocates a new Thread object.
        //new Thread(() -> {for (int i = 0; i < 51; i++) ticket.sale();}, "c").start();





    }
}
